package org.iori.luodan.logic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.iori.luodan.base.BaseDaoFactory;
import org.iori.luodan.dao.DeviceDao;
import org.iori.luodan.entity.ApnsInfoEntity;
import org.iori.luodan.entity.DeviceInfoEntity;
import org.iori.luodan.entity.UserApnsInfoEntity;

import com.iori.common.constant.ArchConstant;
import com.iori.common.log.FounderLog;
import com.iori.common.log.LogFactory;

/**
 * [クラス名]<br>
 * DeviceLogic.java<br>
 * [機能概要]<br>
 * 検索ロジック<br>
 * <br>
 * [変更履歴]<br>
 * 2012/11/9 ver1.00 新規作成<br>
 * 
 * @author luodan CORPORATION lun_xing
 * @version 1.00
 */
public class DeviceLogic {

    // Log出力クラスを定義
    private static FounderLog log = LogFactory.getFounderLog(DeviceLogic.class);

    // DAO初期化
    private static DeviceDao deviceDao = BaseDaoFactory.getDeviceDao();

    /**
     * コンテンツリストの内容取得
     * <br>
     * @param offset
     * @param limit
     * @param TitleName
     * @return
     * @throws Exception
     */
    public ArrayList getDeviceInfo() throws Exception {
		ArrayList arrayList = new ArrayList();
		List list = deviceDao.getDeviceInfo();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				HashMap map = (HashMap)list.get(i);
				DeviceInfoEntity entity = this.mapToDeviceInfoEntity(map);
				if (null != entity) {
					arrayList.add(entity);
				}
		    }
		}
		return arrayList;
    }
    
    private DeviceInfoEntity mapToDeviceInfoEntity(HashMap map) {
    	if (null == map) {
    		return null;
    	}
    	Object obj = null;
    	obj = map.get("USER_ID");
    	int user_id = Integer.valueOf(ArchConstant.DEFAULT_USER_ID);
    	if (null != obj) {
    		user_id = Integer.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("DEVICE_TOKEN");
    	String device_token = "";
    	if (null != obj) {
    		device_token = obj.toString().trim();
    	}

    	DeviceInfoEntity entity = new DeviceInfoEntity();

    	entity.setUser_id(user_id);
    	entity.setDevice_token(device_token);

    	return entity;
    }
    
    /**
     * 検索指定ユーザのApns infoの情報
     * <br>
     * @param offset
     * @param limit
     * @param TitleName
     * @return
     * @throws Exception
     */
    public ArrayList getUserApnsInfo(int userId) throws Exception {
		ArrayList arrayList = new ArrayList();
		if (userId == Integer.valueOf(ArchConstant.DEFAULT_USER_ID)) {
			return arrayList;
		}
		List list = deviceDao.getUserApnsInfo(userId);
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				HashMap map = (HashMap)list.get(i);
				UserApnsInfoEntity entity = this.mapToUserApnsInfoEntity(map);
				if (null != entity) {
					arrayList.add(entity);
				}
		    }
		}
		return arrayList;
    }
    
    private UserApnsInfoEntity mapToUserApnsInfoEntity(HashMap map) {
    	if (null == map) {
    		return null;
    	}
    	Object obj = null;    	
    	obj = map.get("DEVICE_TOKEN");
    	String device_token = "";
    	if (null != obj) {
    		device_token = obj.toString().trim();
    	}
    	
    	obj = map.get("USER_NAME");
    	String user_name = "";
    	if (null != obj) {
    		user_name = obj.toString().trim();
    	}
    	
    	obj = map.get("MESSAGE");
    	String message = "";
    	if (null != obj) {
    		message = obj.toString().trim();
    	}

    	UserApnsInfoEntity entity = new UserApnsInfoEntity();

    	entity.setDevice_token(device_token);
    	entity.setUser_name(user_name);
    	entity.setMessage(message);

    	return entity;
    }
    
    /**
     * コンテンツリストの内容取得
     * <br>
     * @param offset
     * @param limit
     * @param TitleName
     * @return
     * @throws Exception
     */
    public ApnsInfoEntity getApnsInfo() throws Exception {
    	HashMap map = deviceDao.getApnsInfo();
    	ApnsInfoEntity entity = this.mapToApnsInfoEntity(map);
		return entity;
    }
    
    private ApnsInfoEntity mapToApnsInfoEntity(HashMap map) {
    	if (null == map) {
    		return null;
    	}
    	Object obj = null;
    	obj = map.get("CERTIFICATE_FILE");
    	String certificate_file = "";
    	if (null != obj) {
    		certificate_file = obj.toString().trim();
    	}
    	
    	obj = map.get("PASS_WORD");
    	String pass_word = "";
    	if (null != obj) {
    		pass_word = obj.toString().trim();
    	}
    	
    	obj = map.get("MESSAGE");
    	String message = "";
    	if (null != obj) {
    		message = obj.toString().trim();
    	}
    	
    	obj = map.get("USER_ID");
    	int user_id = Integer.valueOf(ArchConstant.DEFAULT_USER_ID);
    	if (null != obj) {
    		user_id = Integer.valueOf(obj.toString().trim());
    	}
    	
    	obj = map.get("IS_SEND");
    	Boolean is_send = false;
    	if (null != obj) {
    		is_send = Boolean.valueOf(obj.toString().trim());
    	}

    	ApnsInfoEntity entity = new ApnsInfoEntity();

    	entity.setCertificate_file(certificate_file);
    	entity.setPass_word(pass_word);
    	entity.setMessage(message);
    	entity.setUser_id(user_id);
    	entity.setIs_send(is_send);
    	
    	return entity;
    }
    
    /**
     * コンテンツリストの内容insertOrReplace
     * <br>
     * @param offset
     * @param limit
     * @param TitleName
     * @return
     * @throws Exception
     */
    public Boolean insertOrReplaceDeviceInfo(String deviceToken, int userId) throws Exception {
    	if (null == deviceToken || "".equals(deviceToken)) {
    		return false;
    	}
    	
    	Boolean result = deviceDao.isDeviceExist(deviceToken);
    	
    	if (true == result) {
    		if (-1 == userId) {
    			return result;
    		}
    		result = deviceDao.updateDeviceInfo(deviceToken, userId);
    		return result;
    	}
    	
    	if (-1 == userId) {
    		result = deviceDao.insertDeviceInfo(deviceToken);
    		return result;
    	}
    	
		result = deviceDao.insertDeviceInfo(deviceToken, userId);
		return result;
    }
}
